import * as actionType from './action-type';
import { reqmain,reqinit,reqswiper} from '../../../api/index'


//轮播图页面数据初始化
export const initSwiper = (data:Array<string>)=>{
    return{
        type:actionType.INIT_SWIPER,
        data:data
    }
}
export const getInitSwiper = ()=>{
    return(dispatch:Function)=>{        
        reqswiper()
            .then(
                (res)=>{
                    let {data}  = res as {data:{data:Array<string>}}
                    dispatch(initSwiper(data.data))
                }
            ).catch(err=>{
                console.log(err,'请求出错了');
            })
    }
}

//数据初始化
export const initData = (data:Object)=>{
    return {
        type:actionType.INIT_DESIGN,
        data:data
    }
}
export const getInitData = ()=>{
    return (dispatch:Function)=>{
        reqinit()
            .then(
                (res)=>{
                    let {data}  = res as {data:{data:Object}}
                    dispatch(initData(data.data))
                }
            ).catch(err=>{
                console.log(err,'请求出错');
                
            })
    }
}

//主要数据
export const changeMainData = (data:Object) => {
    return {
        type: actionType.CHANGE_MAINDATA,
        data: data
    }
}
export const getMainData = (config:string,color:string,tyre:string,interior:string) => {
    // api
    return (dispatch:Function) => {
        reqmain({config,color,tyre,interior},'GET')
            .then((res) => {
                // console.log(res);
                // dispatch 同步action
               let {data}  = res as {data:{data:Object}}
                dispatch(changeMainData(data.data))
            }).catch((err) => {
                console.log(err,'出错了');
            });
    }
}